package com.tamartech.greendb.data;

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

import com.tamartech.greendb.importers.ElectoralRollImporter;

@Entity
@Table(name = "address")
public class Address
{
    private static Logger log = Logger.getLogger (Address.class);
    
    @Id
    @GeneratedValue
    private long id;

    // initialising to empty string rather than null, as it makes retrieval in HQL/SQL easier
    
    @Column(name = "address1", nullable = true)
    protected String address1 = "";

    @Column(name = "address2", nullable = true)
    protected String address2 = "";

    @Column(name = "address3", nullable = true)
    protected String address3 = "";

    @Column(name = "address4", nullable = true)
    protected String address4 = "";

    @Column(name = "address5", nullable = true)
    protected String address5 = "";

    @Column(name = "postcode", nullable = false)
    protected String postcode;

    @ManyToOne
    @JoinColumn(name="first_seen_roll_id", nullable = true)
    protected ElectoralRoll firstSeenRoll;
    
    public String getAddress4()
    {
        return address4;
    }

    public void setAddress4(String address4)
    {
        this.address4 = address4;
    }

    public String getAddress5()
    {
        return address5;
    }

    public void setAddress5(String address5)
    {
        this.address5 = address5;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) 
        {
            return false;
        }
        Address rhs = (Address) obj;

        boolean ret = new EqualsBuilder()
//                     .appendSuper(super.equals(obj))
                     .append(getAddress1(), rhs.getAddress1())
                     .append(getAddress2(), rhs.getAddress2())
                     .append(getAddress3(), rhs.getAddress3())
                     .append(getAddress4(), rhs.getAddress4())
                     .append(getAddress5(), rhs.getAddress5())
                     .append(getPostcode(), rhs.getPostcode())
                     .isEquals();
        
        log.info ("ret: " + ret + ". this: " + this + ". rhs: " + rhs);
        
        return ret;
    }

    
    @Override
    public int hashCode()
    {
        int hc = new HashCodeBuilder (17, 37)
                     .append(getAddress1())
                     .append(getAddress2())
                     .append(getAddress3())
                     .append(getAddress4())
                     .append(getAddress5())
                     .append(getPostcode())
                     .toHashCode();
        
        log.info ("hc: " + hc + ". this: " + this);
        
        return hc;
    }

    @Override
    public String toString()
    {
        return getAddress1() + "," + getAddress2() + "," + getAddress3() + "," + getAddress4() + "," + getAddress5() + "," + getPostcode();
    }

    public String getAddress1()
    {
        return address1;
    }

    public void setAddress1(String address1)
    {
        this.address1 = address1;
    }

    public String getAddress2()
    {
        return address2;
    }

    public void setAddress2(String address2)
    {
        this.address2 = address2;
    }

    public String getAddress3()
    {
        return address3;
    }

    public void setAddress3(String address3)
    {
        this.address3 = address3;
    }

    public String getPostcode()
    {
        return postcode;
    }

    public void setPostcode(String postcode)
    {
        this.postcode = postcode;
    }

    public long getId()
    {
        return id;
    }

    public ElectoralRoll getFirstSeenRoll()
    {
        return firstSeenRoll;
    }

    public void setFirstSeenRoll(ElectoralRoll firstSeenRoll)
    {
        this.firstSeenRoll = firstSeenRoll;
    }

   
}